package handler

import (
	"chap-manage/models"
	"chap-manage/pkg"
	"github.com/gin-gonic/gin"
)

func Logan(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	var news models.User
	err := models.DB.Where("username =?", username).Find(&news).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户或密码错误",
		})
		return
	}
	if news.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if news.Password != pkg.Md5(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
